﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SwParaPlatform2
{
    public static class WindowAlignmentHelper
    {
        public enum Alignment
        {
            TopCenter,
            BottomCenter,
            LeftCenter,
            RightCenter,
            Center,
            TopLeft,
            TopRight,
            BottomLeft,
            BottomRight
        }

        /// <summary>
        /// 根据指定的对齐方式，将子窗口与主窗口对齐。
        /// </summary>
        /// <param name="mainWindow">主窗口的引用。</param>
        /// <param name="childWindow">子窗口的引用。</param>
        /// <param name="alignment">对齐方式。</param>
        public static void Align(Form mainWindow, Form childWindow, Alignment alignment)
        {
            // 确保子窗口的启动位置为手动设置
            childWindow.StartPosition = FormStartPosition.Manual;

            // 根据对齐方式计算子窗口的位置
            int x = mainWindow.Location.X;
            int y = mainWindow.Location.Y;

            switch (alignment)
            {
                case Alignment.TopCenter:
                    x += (mainWindow.Width - childWindow.Width) / 2;
                    break;
                case Alignment.BottomCenter:
                    x += (mainWindow.Width - childWindow.Width) / 2;
                    y += mainWindow.Height - childWindow.Height;
                    break;
                case Alignment.LeftCenter:
                    y += (mainWindow.Height - childWindow.Height) / 2;
                    break;
                case Alignment.RightCenter:
                    x += mainWindow.Width - childWindow.Width;
                    y += (mainWindow.Height - childWindow.Height) / 2;
                    break;
                case Alignment.Center:
                    x += (mainWindow.Width - childWindow.Width) / 2;
                    y += (mainWindow.Height - childWindow.Height) / 2;
                    break;
                case Alignment.TopLeft:
                    // already set by default
                    break;
                case Alignment.TopRight:
                    x += mainWindow.Width - childWindow.Width;
                    break;
                case Alignment.BottomLeft:
                    y += mainWindow.Height - childWindow.Height;
                    break;
                case Alignment.BottomRight:
                    x += mainWindow.Width - childWindow.Width;
                    y += mainWindow.Height - childWindow.Height;
                    break;
            }

            // 设置子窗口的位置
            childWindow.Location = new Point(x, y);
        }
    }

}
